// 幻灯片相关变量
let slideIndex = 0;
let slideInterval;

// 页面加载完成后初始化
document.addEventListener('DOMContentLoaded', function() {
    showSlides();
    startSlideShow();
    
    // 处理URL锚点
    handleUrlAnchor();
});

// 显示幻灯片
function showSlides() {
    const slides = document.querySelectorAll('.slide');
    const dots = document.querySelectorAll('.dot');
    
    // 隐藏所有幻灯片
    slides.forEach(slide => {
        slide.classList.remove('active');
    });
    
    // 移除所有点的激活状态
    dots.forEach(dot => {
        dot.classList.remove('active');
    });
    
    // 显示当前幻灯片
    if (slides[slideIndex]) {
        slides[slideIndex].classList.add('active');
    }
    
    // 激活当前点
    if (dots[slideIndex]) {
        dots[slideIndex].classList.add('active');
    }
}

// 切换到下一张幻灯片
function changeSlide(n) {
    const slides = document.querySelectorAll('.slide');
    slideIndex += n;
    
    if (slideIndex >= slides.length) {
        slideIndex = 0;
    }
    if (slideIndex < 0) {
        slideIndex = slides.length - 1;
    }
    
    showSlides();
    resetSlideInterval();
}

// 直接跳转到指定幻灯片
function currentSlide(n) {
    slideIndex = n - 1;
    showSlides();
    resetSlideInterval();
}

// 开始自动播放
function startSlideShow() {
    slideInterval = setInterval(() => {
        changeSlide(1);
    }, 4000); // 每4秒切换一次
}

// 重置自动播放计时器
function resetSlideInterval() {
    clearInterval(slideInterval);
    startSlideShow();
}

// 菜单切换功能
function showSection(sectionId) {
    // 隐藏所有内容区域
    const sections = document.querySelectorAll('.content-section');
    sections.forEach(section => {
        section.classList.remove('active');
    });
    
    // 移除所有导航项的激活状态
    const navItems = document.querySelectorAll('.nav-item');
    navItems.forEach(item => {
        item.classList.remove('active');
    });
    
    // 显示选中的内容区域
    const targetSection = document.getElementById(sectionId);
    if (targetSection) {
        targetSection.classList.add('active');
    }
    
    // 激活对应的导航项
    const targetNavItem = document.querySelector(`[onclick="showSection('${sectionId}')"]`);
    if (targetNavItem) {
        targetNavItem.classList.add('active');
    }
    
    // 平滑滚动到内容区域，考虑菜单栏的高度
    setTimeout(() => {
        const navigationHeight = document.querySelector('.navigation').offsetHeight;
        const targetPosition = targetSection.offsetTop - navigationHeight - 20; // 20px的额外间距
        
        window.scrollTo({
            top: targetPosition,
            behavior: 'smooth'
        });
    }, 100);
}

// 鼠标悬停时暂停幻灯片自动播放
document.querySelector('.slideshow-container').addEventListener('mouseenter', function() {
    clearInterval(slideInterval);
});

// 鼠标离开时恢复幻灯片自动播放
document.querySelector('.slideshow-container').addEventListener('mouseleave', function() {
    startSlideShow();
});

// 为图片添加加载错误处理
document.addEventListener('DOMContentLoaded', function() {
    const images = document.querySelectorAll('img');
    images.forEach(img => {
        img.addEventListener('error', function() {
            // 如果图片加载失败，显示占位符
            this.src = '';
        });
    });
});

// 移除页面滚动效果，让幻灯片正常滚动
// window.addEventListener('scroll', function() {
//     const scrolled = window.pageYOffset;
//     const parallax = document.querySelector('.header');
//     const speed = scrolled * 0.5;
//     
//     if (parallax) {
//         parallax.style.transform = `translateY(${speed}px)`;
//     }
// });

// 添加加载动画
function addLoadingAnimation() {
    const sections = document.querySelectorAll('.content-section');
    sections.forEach(section => {
        const items = section.querySelectorAll('.gallery-item, .article-item, .video-item, .artwork-item, .project-item');
        items.forEach((item, index) => {
            item.style.opacity = '0';
            item.style.transform = 'translateY(30px)';
            item.style.transition = 'opacity 0.6s ease, transform 0.6s ease';
            
            setTimeout(() => {
                item.style.opacity = '1';
                item.style.transform = 'translateY(0)';
            }, index * 100);
        });
    });
}

// 打开视频播放页面
function openVideo(videoSrc, title, author, className, duration) {
    // 将视频信息存储到 localStorage，供视频页面使用
    const videoInfo = {
        src: videoSrc,
        title: title,
        author: author,
        className: className,
        duration: duration
    };
    localStorage.setItem('videoInfo', JSON.stringify(videoInfo));
    
    // 跳转到视频播放页面
    window.open('video.html', '_blank');
}

// 打开照片查看页面
function openPhoto(photoSrc, title, author, className, description) {
    // 将照片信息存储到 localStorage，供照片页面使用
    const photoInfo = {
        src: photoSrc,
        title: title,
        author: author,
        className: className,
        description: description
    };
    localStorage.setItem('photoInfo', JSON.stringify(photoInfo));
    
    // 跳转到照片查看页面
    window.open('photo.html', '_blank');
}

// 打开文章页面
function openArticle(articlePage, title, author, className, publishDate) {
    // 将文章信息存储到 localStorage，供文章页面使用
    const articleInfo = {
        title: title,
        author: author,
        className: className,
        publishDate: publishDate
    };
    localStorage.setItem('articleInfo', JSON.stringify(articleInfo));
    
    // 跳转到文章页面
    window.open(articlePage, '_blank');
}

// 打开科技项目页面
function openProject(projectPage, title, author, className) {
    // 将项目信息存储到 localStorage，供项目页面使用
    const projectInfo = {
        title: title,
        author: author,
        className: className
    };
    localStorage.setItem('projectInfo', JSON.stringify(projectInfo));
    
    // 跳转到项目页面
    window.open(projectPage, '_blank');
}

// 当内容区域变为可见时添加动画
const observer = new IntersectionObserver((entries) => {
    entries.forEach(entry => {
        if (entry.isIntersecting) {
            addLoadingAnimation();
            observer.unobserve(entry.target);
        }
    });
}, { threshold: 0.1 });

// 观察所有内容区域
document.addEventListener('DOMContentLoaded', function() {
    const sections = document.querySelectorAll('.content-section');
    sections.forEach(section => {
        observer.observe(section);
    });
});

// 处理URL锚点
function handleUrlAnchor() {
    const hash = window.location.hash;
    if (hash) {
        const sectionId = hash.substring(1); // 移除#号
        const targetSection = document.getElementById(sectionId);
        
        if (targetSection) {
            // 延迟执行，确保页面完全加载
            setTimeout(() => {
                showSection(sectionId);
            }, 500);
        }
    }
}

// 监听URL变化
window.addEventListener('hashchange', function() {
    handleUrlAnchor();
});
